import {
    reactive
} from "vue";
import { onPullDownRefresh, onReachBottom, onReady, onShow } from "@dcloudio/uni-app";
import { useStore } from "/@/cool";
const { user } = useStore();

const listMixin = (value: any | {
    apiFn: any;
    query: {};
    init?: Boolean;
    showInit?: Boolean,
    callFn?: null;
    isDefaultRefreshLoad?: Boolean
    isDefaultReachLoad?: Boolean
    isList?: Boolean
}) => {

    const listData = reactive<any>({
        list: [],
        page: 1,
        size: 20,
        total: 0,
        load: false,
        finish: false,
        apiFn: value.apiFn,
        query: value.query || {},
        init: false,
        showInit: value.showInit === false ? false : true,
        isDefaultRefreshLoad: true,
        isDefaultReachLoad: true,
        isList: value.isList || false,

        // 业务相关
        sumQty: 0,
        sumVolume: 0,
        sumWeight: 0,
        totalQty: 0,
        totalVolume: 0,

        sumArea: 0,
        totalArea: 0,
        totalTurnsQty: 0
    })

    async function getList(cValue?: any | {
        apiFn?: null;
        page: number,
        query?: null;
        callFn?: null
    }) {
        if (cValue?.apiFn) {
            listData.apiFn = cValue.apiFn
        }

        if (cValue?.page) {
            listData.page = cValue.page
        }

        if (listData.page == 1) {
            resize()
        }

        if (cValue?.query) {
            listData.query = cValue.query
        }

        const data = await listData.apiFn({
            pageNum: listData.page,
            pageSize: listData.size,
            ...listData.query,
        })

        // 报表
        listData.erpName = data?.erpName || ''
        // 荒料
        listData.sumQty = data?.sumQty || 0
        listData.sumVolume = data?.sumVolume || 0
        listData.sumWeight = data?.sumWeight || 0
        listData.totalQty = data?.totalQty || 0
        listData.totalVolume = data?.totalVolume || 0
        // 大板
        listData.totalArea = data?.totalArea || 0
        listData.sumArea = data?.sumArea || 0
        // listData.sumDedArea = data?.sumDedArea || 0
        // listData.sumPreArea = data?.sumPreArea || 0
        // listData.sumQty = data?.sumQty || 0
        listData.totalTurnsQty = data?.totalTurnsQty || 0

        // let newList = data?.dataList ? data?.dataList : data ? data : []
        
        let newList: any[] = []
        // 是否强制需要接口返回list才渲染
        if (listData.isList) {
            newList = data?.dataList ? data?.dataList : []
        } else {
            newList = data?.dataList ? data?.dataList : data ? data : []
        }
        
        if (value.callFn) {
            newList = await value.callFn(newList)
        }

        if (data.page === 1 || listData.page === 1) {
            listData.list = newList
        } else {
            const _allList = JSON.parse(JSON.stringify(listData.list))
            listData.list = [..._allList, ...newList]
        }

        listData.total = data?.totalSize ?? 0

        // if (data.page >= data.totalPages) {
        if (newList.length < listData.size) {
            listData.finish = true
        }
        
        listData.page = listData.page + 1

        return data
    }

    function resize() {
        listData.total = 0
        listData.page = 1
        listData.finish = false
    }

    onReady(() => {
        if (!listData.init) return false

        getList()
    })

    onShow(() => {
        if (!listData.showInit) return false

        resize()
        getList()
    })

    onPullDownRefresh(async () => {
        if (!listData.isDefaultRefreshLoad) return false

        console.info("触发了")
        resize()

        getList().finally(() => {
            console.info("下拉刷新完成了！")
            setTimeout(() => {
                uni.stopPullDownRefresh();
            }, 500)
        })
    });

    onReachBottom(async () => {
        if (!listData.isDefaultReachLoad) return false

        if (listData.finish) return false

        listData.load = true

        getList().finally(() => {
            listData.load = false
        })
    });

    return {
        listData,

        getList
    };
}

export default listMixin